﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Juece.Report
{
    public partial class 月主要产品产量表 : System.Web.UI.Page
    {
        protected double[] 计划量 = new double[12];//计划量
        protected double[] 实际量 = new double[12];//实际量
        protected double[] 本月止累计量 = new double[12];//本月止累计量
        protected double[] 完成率 = new double[12];//完成率

        protected string 品名;
        protected string 钢级;
        protected string 单位;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {                
                this.Year0.SelectedValue = DateTime.Today.Year.ToString();
            }
        }
        protected double 月计划量(int month,string 品名ID)
        {
            double 计划量 = 0;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            SqlCommand Cmd = new SqlCommand();
            Cmd.Connection = Conn;
            if (品名ID == "1")
            {
                string sql = @"select 油管 from 生产计划2 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }
            if (品名ID == "2")
            {
                string sql = @"select 套管 from 生产计划2 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }
            if (品名ID == "3")
            {
                string sql = @"select 油管接箍 from 生产计划2 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }
            if (品名ID == "4")
            {
                string sql = @"select 套管接箍 from 生产计划2 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }
            if (品名ID == "5")
            {
                string sql = @"select 套管保护器 from 生产计划2 where 月份=" + month + "and 年度=" + this.Year0.SelectedValue;
                Cmd.CommandText = sql;
            }   
            //---------n80
            if (品名ID == "6")
            {
                string sql = @"
                declare @油管N80计划 float
                select @油管N80计划";
                Cmd.CommandText = sql;
            }
            if (品名ID == "7")
            {
                string sql = @"
                declare @套管N80计划 float
                select @套管N80计划";
                Cmd.CommandText = sql;
            }
            if (品名ID == "8")
            {
                string sql = @"
                declare @油管N80接箍计划 float
                select @油管N80接箍计划";
                Cmd.CommandText = sql;
            }
            if (品名ID == "9")
            {
                string sql = @"
                declare @套管N80接箍计划 float
                select @套管N80接箍计划";
                Cmd.CommandText = sql;
            }
            SqlDataReader Reader = Cmd.ExecuteReader();
            if (Reader.Read())
            {
                if (Reader.IsDBNull(0))
                {
                    计划量 = 0;
                }
                else
                {
                    计划量 = Reader.GetDouble(0);
                }
            }
            Reader.Close();
            Conn.Close();
            Reader.Dispose();
            Cmd.Dispose();
            Conn.Dispose();
            return 计划量;
        }
        //月实际量
        protected double 月实际量(int month,string 品名ID)
        {
            double 实际量 = 0;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            try
            {
                Conn.Open();
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Conn;
                if (品名ID == "2")
                {
                    string sql = "select dbo.产品本月实际产量(*年份*,*月份*,2)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                if (品名ID == "1")
                {
                    string sql = "select dbo.产品本月实际产量(*年份*,*月份*,1)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }

                if (品名ID == "3")
                {
                    string sql = "select dbo.产品本月实际产量(*年份*,*月份*,3)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                if (品名ID == "4")
                {
                    string sql = "select dbo.产品本月实际产量(*年份*,*月份*,4)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                if (品名ID == "5")
                {
                    string sql = "select dbo.产品本月实际产量(*年份*,*月份*,5)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                //-----------n80
                if (品名ID == "7")
                {
                    string sql = "select dbo.产品本月实际产量(*年份*,*月份*,7)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                if (品名ID == "6")
                {
                    string sql = "select dbo.产品本月实际产量(*年份*,*月份*,6)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }

                if (品名ID == "8")
                {
                    string sql = "select dbo.产品本月实际产量(*年份*,*月份*,8)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                if (品名ID == "9")
                {
                    string sql = "select dbo.产品本月实际产量(*年份*,*月份*,9)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
               
                SqlDataReader Reader = Cmd.ExecuteReader();
                if (品名ID == "5")
                {
                    if (Reader.Read())
                    {
                        if (Reader.IsDBNull(0))
                        {
                            实际量 = 0;
                        }
                        else
                        {
                            实际量 = Reader.GetInt32(0);
                        }
                    }
                }
                else
                {
                    if (Reader.Read())
                    {
                        if (Reader.IsDBNull(0))
                        {
                            实际量 = 0;
                        }
                        else
                        {
                            实际量 = double.Parse(Reader.GetDecimal(0).ToString());
                        }
                    }
                }
                Reader.Close();
                Reader.Dispose();
                Cmd.Dispose();
            }
            catch (Exception ex)
            {
                Conn.Close();
                throw ex;
            }
            finally
            {               
                Conn.Close();               
                Conn.Dispose();
            }
            return 实际量;
        }
        //本月止完成量
        protected double 本月止完成量(int month, string 品名ID)
        {
            double 本月止完成量 = 0;
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
            Conn.Open();
            try
            {
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Conn;
                if (品名ID == "2")
                {
                    string sql = "select dbo.产品某月止累计(*年份*,*月份*,2)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                if (品名ID == "1")
                {
                    string sql = "select dbo.产品某月止累计(*年份*,*月份*,1)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }

                if (品名ID == "3")
                {
                    string sql = "select dbo.产品某月止累计(*年份*,*月份*,3)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                if (品名ID == "4")
                {
                    string sql = "select dbo.产品某月止累计(*年份*,*月份*,4)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                if (品名ID == "5")
                {
                    string sql = "select dbo.产品某月止累计(*年份*,*月份*,5)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                //--------------n80
                if (品名ID == "7")
                {
                    string sql = "select dbo.产品某月止累计(*年份*,*月份*,7)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                if (品名ID == "6")
                {
                    string sql = "select dbo.产品某月止累计(*年份*,*月份*,6)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }

                if (品名ID == "8")
                {
                    string sql = "select dbo.产品某月止累计(*年份*,*月份*,8)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                if (品名ID == "9")
                {
                    string sql = "select dbo.产品某月止累计(*年份*,*月份*,9)";
                    sql = sql.Replace("*年份*", this.Year0.SelectedValue.ToString());
                    sql = sql.Replace("*月份*", month.ToString());
                    Cmd.CommandText = sql;
                }
                SqlDataReader Reader = Cmd.ExecuteReader();
                if (品名ID == "5")
                {
                    if (Reader.Read())
                    {
                        if (Reader.IsDBNull(0))
                        {
                            本月止完成量 = 0;
                        }
                        else
                        {
                            本月止完成量 = Reader.GetInt32(0);
                        }
                    }
                }
                else
                {
                    if (Reader.Read())
                    {
                        if (Reader.IsDBNull(0))
                        {
                            本月止完成量 = 0;
                        }
                        else
                        {
                            本月止完成量 = double.Parse(Reader.GetDecimal(0).ToString());
                        }
                    }
                }
                Reader.Close();
                Reader.Dispose();
                Cmd.Dispose();
            }
            catch (Exception ex)
            {
                Conn.Close();
                throw ex;
            }
            finally
            {                
              Conn.Close();        
              Conn.Dispose();
            }
           
            return 本月止完成量;
        }

        protected void Button_Query_Click(object sender, EventArgs e)
        {
             品名= this.PinMing.SelectedItem.Text;
            string 品名ID = this.PinMing.SelectedItem.Value;
            if (品名ID == "1" || 品名ID == "2")
            {
                钢级 = "J55";
            }
            else if (品名ID == "6" || 品名ID == "7")
            {
                钢级 = "N80";
            }
            else
            {
                钢级 = "-";
            }
            if (品名ID == "1" || 品名ID == "2" || 品名ID == "6" || 品名ID == "7")
            {
                单位 = "吨";
            }
            else if (品名ID == "3" || 品名ID == "4" || 品名ID == "8" || 品名ID == "9")
            {
                单位 = "支";
            }
            else
            {
                单位 = "套";
            }
            //计划量
            for (int i = 0; i < 12; i++)
            {
                计划量[i] = 月计划量(i + 1,this.PinMing.SelectedItem.Value);
            }
            //实际量
            for (int i = 0; i < 12; i++)
            {
                实际量[i] = 月实际量(i + 1, this.PinMing.SelectedItem.Value);
            }
            //本月止完成量
            for (int i = 0; i < 12; i++)
            {
                本月止累计量[i] = 本月止完成量(i + 1, this.PinMing.SelectedItem.Value);
            }
            //完成率            
            for (int i = 0; i < 12; i++)
            {
                if (计划量[i] == 0)
                {
                    完成率[i] = 0;
                }
                else
                {
                    完成率[i] = 实际量[i] / 计划量[i];
                }
            }
        }
     

    }
}
